/**
   VideoMessageServiceImpl
 * Time:2018年5月25日
 */
package com.ssm.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.ssm.dao.HotmsgMapper;
import com.ssm.dao.LabelmsgMapper;
import com.ssm.dao.VideomessageMapper;
import com.ssm.dao.VideoshowareaMapper;
import com.ssm.dao.VideotypemessageMapper;
import com.ssm.entity.Hotmsg;
import com.ssm.entity.HotmsgExample;
import com.ssm.entity.Labelmsg;
import com.ssm.entity.LabelmsgExample;
import com.ssm.entity.Page;
import com.ssm.entity.Videomessage;
import com.ssm.entity.VideomessageExample;
import com.ssm.entity.VideomessageExample.Criteria;
import com.ssm.entity.VideomessageExample.Criterion;
import com.ssm.entity.Videoshowarea;
import com.ssm.entity.VideoshowareaExample;
import com.ssm.entity.Videotypemessage;
import com.ssm.entity.VideotypemessageExample;
import com.ssm.entity.vo.PieChartVo;
import com.ssm.entity.vo.SeachByColumn;
import com.ssm.entity.vo.VideoMessageVo;
import com.ssm.entity.vo.VideomessageHotVo;
import com.ssm.entity.vo.VideoshowVo;
import com.ssm.service.VideoMessageService;
/**
 * @author 15199
 */
@SuppressWarnings("all")
@Service("videoMessageService")
public class VideoMessageServiceImpl implements VideoMessageService {
	@Resource
	private VideomessageMapper videomessageMapper;
	@Resource
	private VideotypemessageMapper videotypemessageMapper;
	@Resource
	private VideoshowareaMapper videoshowareaMapper;
	@Resource
	private HotmsgMapper hotmsgMapper;
	@Resource
	private LabelmsgMapper labelmsgMapper;
	@Override
	public List<VideoMessageVo> getVmBycondition(Page page,SeachByColumn seachByColumn) {
		VideomessageExample videomessageExample=new VideomessageExample();
		videomessageExample.setPage(page);
		videomessageExample.setDistinct(true);
		Criteria criteria=videomessageExample.createCriteria();
		if(seachByColumn.getVideoid()!=null){
			criteria.andVideoidEqualTo(seachByColumn.getVideoid());
		}
		if(seachByColumn.getVideoname()!=null){
			criteria.andVideonameLike("%"+seachByColumn.getVideoname()+"%");	 
		}
		return videomessageMapper.getVmBycondition(videomessageExample);
	}
	@Override
	public List<VideoMessageVo> getCount() {
		VideomessageExample videomessageExample=new VideomessageExample();
		videomessageExample.setDistinct(true);
		videomessageExample.setCount(true);
		Criteria criteria=videomessageExample.createCriteria();
		return videomessageMapper.getVmBycondition(videomessageExample);
	}
	@Override
	public void updateBycondition(VideoMessageVo videoMessageVo) {
		VideomessageExample videomessageExample=new VideomessageExample();
		VideotypemessageExample videotypemessageExample=new VideotypemessageExample();
		VideoshowareaExample videoshowareaExample=new VideoshowareaExample();
		HotmsgExample hotmsgExample=new HotmsgExample();
		LabelmsgExample labelmsgExample=new LabelmsgExample();
		Criteria criteria0=videomessageExample.createCriteria();
		com.ssm.entity.VideotypemessageExample.Criteria criteria=videotypemessageExample.createCriteria();
		com.ssm.entity.VideoshowareaExample.Criteria criteria1=videoshowareaExample.createCriteria();
		com.ssm.entity.HotmsgExample.Criteria criteria2=hotmsgExample.createCriteria();
		com.ssm.entity.LabelmsgExample.Criteria criteria3=labelmsgExample.createCriteria();
		Videomessage videomessage=new Videomessage(videoMessageVo.getVideoname(),videoMessageVo.getVideorecommend(),videoMessageVo.getReleasetime(),videoMessageVo.getVideostate());
		videomessageMapper.updateByExampleSelective(videomessage,videomessageExample);    
		criteria.andVideoidEqualTo(videoMessageVo.getVideoid());     
		Videotypemessage videotypemessage=new Videotypemessage(videoMessageVo.getVideotype());
		videotypemessageMapper.updateByExampleSelective(videotypemessage,videotypemessageExample);
		criteria1.andVideoidEqualTo(videoMessageVo.getVideoid());
		Videoshowarea videoshowarea=new Videoshowarea(videoMessageVo.getVideoarea());
		videoshowareaMapper.updateByExampleSelective(videoshowarea, videoshowareaExample);   
		criteria2.andVideoidEqualTo(videoMessageVo.getVideoid());
		Hotmsg hotmsg=new Hotmsg(videoMessageVo.getHot());
		hotmsgMapper.updateByExampleSelective(hotmsg,hotmsgExample);
		criteria3.andVideoidEqualTo(videoMessageVo.getVideoid());
		Labelmsg labelmsg=new Labelmsg(videoMessageVo.getLabelname());
		labelmsgMapper.updateByExampleSelective(labelmsg,labelmsgExample);
	}
	@Override
	public List<VideoMessageVo> getById(int videoid) {
		VideomessageExample videomessageExample=new VideomessageExample();
		Criteria criteria=videomessageExample.createCriteria();
		criteria.andVideoidEqualTo(videoid);
		return videomessageMapper.getVmBycondition(videomessageExample);
	}
	@Override
	public void insert(VideoMessageVo videoMessageVo) {
		VideomessageExample videomessageExample=new VideomessageExample();
		Criteria criteria0=videomessageExample.createCriteria();
		int videoid=0;
		/*
		 * 插入videomessage表中的内容
		 */
		Videomessage videomessage=new Videomessage(videoMessageVo.getVideoname(),videoMessageVo.getVideorecommend(),videoMessageVo.getReleasetime());
		videomessageMapper.insertSelective(videomessage);
		/*
		 *查询最新插入的 
		 *在语句上是倒序用limit取值
		 */
		videomessageExample.setPage(new Page(0,1));
		videomessageExample.setOrderByClause("vm.VideoID DESC");
		List<VideoMessageVo> list=videomessageMapper.getVmBycondition(videomessageExample);
		for (VideoMessageVo videoMessageVo2 : list) {
			videoid=videoMessageVo2.getVideoid()+1;	
		}
		/*
		 * 插入videoshowarea表中的内容
		 */
		Videoshowarea videoshowarea=new Videoshowarea(videoMessageVo.getVideoarea(), videoid);
		videoshowareaMapper.insertSelective(videoshowarea);
		/*
		 * 插入videotypemessage表中的内容
		 */
		String videotype=videoMessageVo.getVideotype();
		Videotypemessage videotypemessage=new Videotypemessage();
		switch (videotype) {
		case "学生向":
			videotypemessage.setVideotypeid(1);
			break;
		case "少年向":
			videotypemessage.setVideotypeid(2);
			break;	
		case "儿童向":
			videotypemessage.setVideotypeid(3);
			break;
		case "少女向":
			videotypemessage.setVideotypeid(4);
			break;
		default:
			break;
		}
		videotypemessage.setVideotype(videotype);
		videotypemessage.setVideoid(videoid);
		videotypemessageMapper.insert(videotypemessage);
		/*
		 * 插入hotmsg表中
		 */
		Hotmsg hotmsg=new Hotmsg(videoMessageVo.getHot(),videoid);
		hotmsgMapper.insert(hotmsg);
		/*
		 * 插入label表中
		 */
		Labelmsg labelmsg=new Labelmsg();
		labelmsg.setVideoid(videoid); 
		labelmsg.setLabelname(videoMessageVo.getLabelname());
		switch (videoMessageVo.getLabelname()) {
		case "中二病":
			labelmsg.setLabelid(1);
			break;
		case "二次元":
			labelmsg.setLabelid(2);
			break;
		case "清新风":
			labelmsg.setLabelid(3);
			break;
		case "萌系风":
			labelmsg.setLabelid(4);
			break;
		default:
			break;
		}
		labelmsgMapper.insert(labelmsg);
	}
	@Override
	public void updateById(int id) {
		VideomessageExample videomessageExample=new VideomessageExample();
		Videomessage videomessage=videomessageMapper.selectByPrimaryKey(id);
		if(videomessage.getVideostate()==1){
			Videomessage videomessage1=new Videomessage(id,0);
			videomessageMapper.updateByPrimaryKeySelective(videomessage1);
		}else{
			Videomessage videomessage1=new Videomessage(id,1);
			videomessageMapper.updateByPrimaryKeySelective(videomessage1);
		}
	}
	@Override
	public List<VideoshowVo> getHomePage(Page page) {
		VideoshowareaExample videoshowareaExample=new VideoshowareaExample();
		com.ssm.entity.VideoshowareaExample.Criteria criteria=videoshowareaExample.createCriteria();
		criteria.andVideoareaLike("%首页%");
		//一般情况下尽量减少||的使用 程序有歧义的可能会增大
		if(page.getBegin()>= 0 && page.getLength()!= 0){
			videoshowareaExample.setPage(page);
		}
		return videoshowareaMapper.getHomePage(videoshowareaExample);
	}
	@Override
	public List<VideoshowVo> getcount() {
		// TODO Auto-generated method stub

		VideoshowareaExample videoshowareaExample=new VideoshowareaExample();
		com.ssm.entity.VideoshowareaExample.Criteria criteria=videoshowareaExample.createCriteria();
		criteria.andVideoareaLike("%首页%");
		videoshowareaExample.setCount(true);
		return videoshowareaMapper.getHomePage(videoshowareaExample);
	}
	@Override
	public List<VideoMessageVo> getTop(Page page) {
		VideomessageExample videomessageExample=new VideomessageExample();
		videomessageExample.setPage(page); 	
		videomessageExample.setOrderByClause("hot desc");
		return videomessageMapper.getVmBycondition(videomessageExample);
	}

	@Override
	public void updateHot(int videoid) {
		int hot=0;
		List<Hotmsg> list=new ArrayList<Hotmsg>();
		HotmsgExample hotmsgExample=new HotmsgExample();
		if(videoid+""!=null){
			hotmsgExample.or().andVideoidEqualTo(videoid);		
			list=hotmsgMapper.selectByExample(hotmsgExample);
		}
		for (Hotmsg hotmsg : list) {
			Hotmsg hotmsg2=new Hotmsg(hotmsg.getHot()+1,hotmsg.getVideoid());
			hotmsgMapper.updateByExampleSelective(hotmsg2,hotmsgExample);         
		}
	}
	@Override
	public List<VideoMessageVo> get_Top(Page page,String Type) {
		// TODO Auto-generated method stub
		VideomessageExample videomessageExample=new VideomessageExample();
		videomessageExample.setPage(page);
		videomessageExample.setOrderByClause("hot desc");
		Map<Integer,Integer> map=new HashMap<Integer, Integer>();
		HotmsgExample hotmsgExample=new HotmsgExample();
		LabelmsgExample labelmsgExample=new LabelmsgExample();
		List<VideoMessageVo> list=new ArrayList<VideoMessageVo>();
		if(Type!=null){
			//		labelmsgExample.or().andLabelnameEqualTo(Type);
			//		
			//		List<Labelmsg> llist=labelmsgMapper.selectByExample(labelmsgExample);
			list=videomessageMapper.getVmBycondition(videomessageExample);
			return list;
		}
		return videomessageMapper.getVmBycondition(videomessageExample);
	}
	@Override
	public int getPageNum() {
		VideomessageExample videomessageExample=new VideomessageExample();
		return videomessageMapper.countByExample(videomessageExample);
	}
	/**最新更新的视频**/
	@Override
	public List<VideoMessageVo> getLastUpdate() {
		VideomessageExample videomessageExample=new VideomessageExample();
		videomessageExample.setPage(new Page(0,3));
		videomessageExample.setOrderByClause("ReleaseTime desc");
		videomessageExample.setDistinct(true);
		return videomessageMapper.getVmBycondition(videomessageExample);

	}

	@Override
	public List<VideoMessageVo> getMostheat() {
		VideomessageExample videomessageExample=new VideomessageExample();
		videomessageExample.setPage(new Page(0,3));
		videomessageExample.setOrderByClause("hot desc");
		videomessageExample.setDistinct(true);
		return videomessageMapper.getVmBycondition(videomessageExample);
	}

	/**数据分析 **/
	@Override
	public List<PieChartVo> getDataAnalysis() {
		VideomessageExample videomessageExample=new VideomessageExample();
		return videomessageMapper.getDataAnalysis();
	}


}











